// pages/news/news.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    unreadNum:0, //未读数量
    newsBtn: [{
        img: '/img/news-btn-1.png',
        title: '我的人脉'
      },
      {
        img: '/img/news-btn-2.png',
        title: '会员企业'
      },
      {
        img: '/img/news-btn-3.png',
        title: '优秀企业'
      },
    ],
    newsList: [
    ],
  },

  click: function(e) {
    let idx = e.currentTarget.dataset.idx;
    app.confirmLogin(
      function() {
        console.log('idx==' + idx)
        let navUrl = '';
        switch (idx) {
          case 0: //我的人脉
            navUrl = 'my-friend/my-friend';
            break;
          case 1: //会员列表
            navUrl = '/pages/company-list/company-list';
            break;
          case 2: //优秀企业
            navUrl = '/pages/great-company/great-company';
            break;

        }
        wx.navigateTo({
          url: navUrl,
        })
      })
  },


  getChatList: function() { //聊天列表
    var that = this;
    var params = {
      user_id: wx.getStorageSync('userId')
    };
    app.request(app.globalData.host + 'chat/index', params, 'GET', false,
      function(res) {
        wx.hideLoading();
        console.log(res)
        var newsList = res.data.data;
        // var sumNews = (newsList || []).map((v, k) => {
        //   return  v.unread++;
        // } )
        // console.log(sumNews[0]);
        
        that.setData({
          // unreadNum: sumNews[0],
          newsList: res.data.data
        })
      },
      function(err) {},
      function(complete) {})
  },

  openChat:function(e){ //打开聊天页面
    var id = e.currentTarget.dataset.id;
    app.confirmLogin(
      function () {
        console.log(id)
        if (id != wx.getStorageSync('userId')) {
          wx.navigateTo({
            url: '/pages/chat/chat?id=' + id,
          })
        }
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!wx.getStorageSync('isLogin')) {
      wx.showModal({
        title: '提示',
        content: '您还未登录，确认登录吗？',
        showCancel: true,
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
      return;
    }
    this.getChatList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showLoading({
      title: '加载中',
    })
    if (!wx.getStorageSync('isLogin')) {
      wx.stopPullDownRefresh()
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        content: '您还未登录，确认登录吗？',
        showCancel: true,
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
      return;
    }
    this.data.newsList = {};
    this.getChatList();
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})